<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<uv-alert type="success" description="此投诉为本小程序自有投诉渠道，非微信官方投诉渠道"></uv-alert>
		<!-- #endif -->
		<view class="p-10">
			<uv-form :model="formData" :rules="rules" ref="ruleForm">
				<uv-form-item label="订单号" prop="order_no">
					<uv-input v-model="formData.order_no" placeholder="请输入订单号" border="none" readonly>
					</uv-input>
				</uv-form-item>
				<uv-form-item label="投诉说明" prop="message">
					<uv-textarea v-model="formData.message" placeholder="请描述具体情况，有助于投诉快速处理。" border="none">
					</uv-textarea>
				</uv-form-item>
				<uv-form-item label="文件上传" prop="fileList">
					<uv-upload :fileList="formData.fileList.img1" name="1" multiple :maxCount="3"
						@afterRead="formData.fileList.afterRead" @delete="formData.fileList.deletePic"></uv-upload>
				</uv-form-item>
				<uv-form-item label="联系电话" prop="phone">
					<uv-input v-model="formData.phone" placeholder="请输入联系电话" border="none">
					</uv-input>
				</uv-form-item>
			</uv-form>
		</view>
		<view class="btn">
			<uv-button text="提交" @click="submit" :loading="loading" shape="circle"
				:color="app.globalData.primaryColor"></uv-button>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue'
	import {
		onLoad,
	} from '@dcloudio/uni-app'
	const app = getApp()
	const formData = reactive({
		order_no: '',
		store_id: '',
		message: '',
		fileList: {
			img1: [],
			imgValid1: false,
			// 删除图片
			deletePic(event) {
				formData.fileList[`img${event.name}`] = formData.fileList[`img${event.name}`].filter((item,
					index) => {
					return index != event.index
				})
				formData.fileList[`imgValid${event.name}`] = true
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					app.uploadFile(url).then(res => {
						console.log(res)
						resolve(res?.url)
					})
				})
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = formData.fileList[`img${event.name}`].length
				lists?.map((item) => {
					formData.fileList[`img${event.name}`].push({
						...item,
						status: '',
						message: '上传中'
					})
				})
				// 此写法可以避免使用老写法在VUE3中不更新
				// #ifdef VUE3
				formData.fileList[`img${event.name}`] = formData.fileList[`img${event.name}`].filter((item,
					index) => {
					return index >= 0
				})
				formData.fileList[`imgValid${event.name}`] = false
				// #endif
				for (let i = 0; i < lists.length; i++) {
					const url = await formData.fileList.uploadFilePromise(lists[i].url)
					let item = formData.fileList[`img${event.name}`][fileListLen]
					formData.fileList[`img${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						code: 'success',
						url: url
					}))
					fileListLen++
				}
			},
		},
		phone: '',
	})
	onLoad((e) => {
		console.log("order_no :", e?.order_no);
		if (!e?.order_no) {
			uni.switchTab({
				url: '/pages/index/index',
			})
		}
		formData.order_no = e?.order_no
		formData.store_id = e?.store_id
	})
	const isPhone = (_, val) => {
		return (/^400(-?)[0-9]{7}$|^1\d{10}$|^0[0-9]{2,3}-[0-9]{7,8}$/.test(val))
	}
	const rules = reactive({
		order_no: [{
			required: true,
			message: '请输入订单号'
		}],
		message: [{
			required: true,
			message: '请填写投诉说明'
		}],
		phone: [{
				required: true,
				message: '请填写联系电话'
			},
			{
				validator: isPhone,
				message: '电话格式不正确'
			},
		],
	})
	const ruleForm = ref(null)
	const loading = ref(false)
	const submit = () => {
		app.vibrate()
		ruleForm.value.validate().then(() => {
			loading.value = true
			const {
				order_no,
				store_id,
				message,
				phone,
				fileList
			} = formData
			const imgs = fileList.img1?.map(item => {
				return item.url
			})
			const data = {
				order_no,
				store_id,
				message,
				phone,
				imgs: imgs.toString(),
			}
			console.log(data);
			app.post('api.auth.complaint/add', data).then(res => {
				console.log('success', res)
				loading.value = false
				uni.navigateBack()
				showToast('投诉信息提交成功')
			}).catch(error => {
				loading.value = false
			})
		}).catch(() => {
			loading.value = false
		})
	}
</script>


<style lang="scss" scoped>
	.p-10 ::v-deep .uv-form {
		gap: 10px;
		display: grid;
		background-color: transparent;

		.uv-form-item {
			background-color: white;
			border-radius: var(--nut-cell-border-radius, 6px);

			.uv-form-item__body {
				padding: 10px;

				.nut-input {
					padding: 0 0 0 5px !important;
					border: none !important;

					.uni-input-placeholder {
						color: rgb(192, 196, 204);
					}
				}

				.uv-form-item__body__left {
					flex: none !important;
					width: var(--nut-form-item-label-width, 90px) !important;
					margin-right: var(--nut-form-item-label-margin-right, 10px);
					font-size: var(--nut-form-item-label-font-size, 14px);
					font-weight: normal;
					text-align: var(--nut-form-item-label-text-align, left);
					word-wrap: break-word;
				}
			}

			.uv-form-item__body__right__message {
				margin-left: var(--nut-form-item-label-width+25px, 90px+25px) !important;
			}
		}

		.uv-textarea {
			padding: 0;
			margin: 0 4px;
		}
	}

	::v-deep .nut-cell-group__wrap {
		gap: 10px;
		display: grid;
		background-color: transparent;

		.nut-form-item {
			border-radius: var(--nut-cell-border-radius, 6px);
		}
	}

	::v-deep .nut-uploader__upload .nut-button--normal {
		display: none;
	}
</style>